#include "SaveSoundInfo.h"

int MapVolume255to9(int src)
{
	if ( src>240 &&  src<=255)
		return 9;
	else if ( src>210 &&  src<=240)
		return 8;
	else if ( src>180 &&  src<=210)
		return 7;
	else if ( src>150 &&  src<=180)
		return 6;
	else if ( src>120 &&  src<=150)
		return 5;
	else if ( src>90 &&  src<=120)
		return 4;
	else if ( src>60 &&  src<=90)
		return 3;
	else if ( src>30 &&  src<=60)
		return 2;
	else if ( src>0 &&  src<=30)
		return 1;
	else if ( src == 0)
		return 0;
	return 9;
}
int MapVolume9to255(int src)
{
	int re = 9;
	switch(src)
	{
	case 0:
		re = 0;
		break;
	case 1:
		re = 30;
		break;
	case 2:
		re = 60;
		break;
	case 3:
		re = 90;
		break;
	case 4:
		re = 120;
		break;
	case 5:
		re = 150;
		break;
	case 6:
		re = 180;
		break;
	case 7:
		re = 210;
		break;
	case 8:
		re = 240;
		break;
	case 9:
		re = 255;
		break;
	}
	return re;
}
void ReadSoundInfo(char pSoundInfoname[],  pSOUNDINFO pSoundInfo,char pVersionInfoname[],INT *pVersion,INT DefaultVersion)
{
	HANDLE	filehandle ;
	DWORD 	filelength ;
	DWORD	reallength ;
	char  	filepath[128];
	TCHAR	File[128];
	ChangePath(filepath, pSoundInfoname);
	GetUnicodeString(File, (LPCSTR)filepath, 0 );
	filehandle =CreateFile((LPCTSTR)File, GENERIC_READ,FILE_SHARE_READ,
		(LPSECURITY_ATTRIBUTES)NULL,OPEN_EXISTING,FILE_ATTRIBUTE_READONLY, (HANDLE)NULL);
	if(filehandle==INVALID_HANDLE_VALUE)
	{
		gm_printf("ERR:Open file lost in ReadSoundInfo %s\r\n",pSoundInfoname);
		pSoundInfo->BKSoundVolume=120;
		pSoundInfo->EffectSoundVolume=120;
		pSoundInfo->PrimaryVolume=95;
		SaveSoundInfo(pSoundInfoname, pSoundInfo ) ;
		pSoundInfo->BKSoundVolume=4;
		pSoundInfo->EffectSoundVolume=4;
	}
	else
	{
		filelength = sizeof(SOUNDINFO);
		ReadFile(filehandle, pSoundInfo, filelength , &reallength,NULL) ;
		pSoundInfo->BKSoundVolume = MapVolume255to9(pSoundInfo->BKSoundVolume);
		pSoundInfo->EffectSoundVolume = MapVolume255to9(pSoundInfo->EffectSoundVolume);
		pSoundInfo->PrimaryVolume = 95;
		CloseFile(filehandle);
	}
}
void SaveSoundInfo(char pname[], pSOUNDINFO pSoundInfo ) 
{

	HANDLE	filehandle ;
	DWORD 	filelength ;
	DWORD	reallength ;
	char  	filepath[128];
	TCHAR	File[128];

	ChangePath(filepath, pname);
	GetUnicodeString(File, (LPCSTR)filepath, 0);
	filehandle =CreateFile((LPCTSTR)File,  GENERIC_WRITE,  FILE_SHARE_WRITE, NULL
		, CREATE_NEW , FILE_ATTRIBUTE_NORMAL, (HANDLE)NULL);
	if (filehandle==INVALID_HANDLE_VALUE)
	{
		filehandle =CreateFile((LPCTSTR)File,  GENERIC_WRITE,  FILE_SHARE_WRITE, NULL
			, OPEN_EXISTING , FILE_ATTRIBUTE_NORMAL, (HANDLE)NULL);
		if(filehandle==INVALID_HANDLE_VALUE)
		{
			gm_printf("ERR:Open file lost in SaveGameData \r\n");
			return;
		}
	}	
	filelength = sizeof(SOUNDINFO) ;
	pSoundInfo->BKSoundVolume = MapVolume9to255(pSoundInfo->BKSoundVolume);
	pSoundInfo->EffectSoundVolume = MapVolume9to255(pSoundInfo->EffectSoundVolume);
	pSoundInfo->PrimaryVolume = 95;
	WriteFile(filehandle, pSoundInfo, filelength , &reallength,NULL) ;
	CloseFile(filehandle) ;
}